from urllib import request
import json
from bs4 import BeautifulSoup  # Beautiful Soup是一个可以从HTML或XML文件中提取结构化数据的Python库
import adwin.cartoon as cartoonDao
import adwin.chapter as chapterDao
import adwin.baseinfo as baseinfo
import adwin.mysqlDemo as mysqlDemo
# 构造头文件，模拟浏览器访问
url = "http://m.adwin.pw/admin/novel/index.html?page=%s"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
    'Cookie':"last_read_notice_id_17=17; PHPSESSID=om9s8hrjlou94u69v2f6vle6c1; SERVERID=43d0834e2b7ed544e40baf15c363bb5f|1559195745|1559195658",
    'Host':'m.adwin.pw',
    'Upgrade-Insecure-Requests': 1
}

def getCartoonIdsByPage(page):
    soup = baseinfo.getUrlContent(url %(page))

    trs = soup.find('div', {"class":"table-responsive clearfix"}).find('tbody').find_all("tr")
    cartoonIds = [];
    for tr in trs:
        try:
            cartoonId = tr.find('input', {"class":"ids"})['value']
            cartoonIds.append(cartoonId)
        except Exception as e:
            break
    return cartoonIds

for pageNum in range(2,cartoonDao.getAllCartoonPages() + 1):

    for cartoonId in getCartoonIdsByPage(pageNum):
        print("开始第%s页的漫画，id=%s" %(pageNum, cartoonId))
        cartoon = cartoonDao.getCartoonDetailByCartoonIdFromWeb(cartoonId)
        book = mysqlDemo.getCartoonById(cartoonId)
        latestChapterNum = 0 #默认从0开始
        if book == None:#漫画不存在，新增漫画首页
            mysqlDemo.insertCartoon(cartoon)
            print("新增漫画《%s》首页完成" %(cartoon['bookname']))
        else:
            latestChapterNum = chapterDao.getLastestNumOfChapter(cartoonId)

        chapterDetailList = chapterDao.selectChaptersByPageAndCheckLastestNumFromWeb(cartoonId, cartoon['chapterPages'], latestChapterNum)

        if chapterDetailList == None or chapterDetailList.__len__() == 0:
            print("《%s》无需要更新的章节" %cartoon['bookname'])
        else:
            for chapter in chapterDetailList:
                mysqlDemo.insertChapter(chapter)
            print("更新《%s》第[%s]章之后的章节完成" %(cartoon['bookname'], latestChapterNum))

